package com.sgb.kjwl.view.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.amap.api.maps.model.LatLng;
import com.sgb.alimaplibrary.util.NavUtil;
import com.sgb.alimaplibrary.widget.MapMarkerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityStorageAddressBinding;
import com.sgb.kjwl.utils.MapUtils;
import com.swgk.core.util.LogUtil;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

public class StorageAddressActivity extends AppActivity {

    ActivityStorageAddressBinding binding;

    public static final String CURRENT_POSITION = "position";
    public static final String TITLE = "title";
    public static final String CITY = "city";
    public static final String LATITUDE = "latitude";
    public static final String LONGTITUDE = "longtitude";

    private static final String TAG = "StorageAddressActivity";
    LatLng mFinalLatLng;
    String city;
    double latitude = 0;
    double longtitude = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(StorageAddressActivity.this, R.layout.activity_storage_address);
        binding.backView.setOnClickListener(this);
        binding.ivDelete.setOnClickListener(this);
        binding.mapBaidu.setOnClickListener(this);
        binding.mapGaode.setOnClickListener(this);
        binding.mapTenxun.setOnClickListener(this);
        binding.txtNav.setOnClickListener(this);
        binding.cancelBtn.setOnClickListener(this);
        binding.llButtomLayout.setOnClickListener(this);
    }

    @Override
    protected void init() {
        initData();
    }

    private void initView(Bundle savedInstanceState) {
        initMap(savedInstanceState);
        initPoiSearch();
    }

    private void initData() {
        if (getIntent() != null) {
            if (getIntent().hasExtra(TITLE)) {
                binding.title.setText(getIntent().getStringExtra(TITLE));
                if ("地图详情".equals(TITLE)) {
                    binding.llButtomLayout.setVisibility(View.GONE);
                }
                city = getIntent().getStringExtra(CITY);
                //默认选中经纬度
                binding.currentPosition.setText(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.PROVINCE_ID, ""));
            }

        }
    }

    private void initMap(Bundle savedInstanceState) {
        binding.mMapView.initMap(savedInstanceState, getLifecycle(), new MapMarkerView.CallBack() {
            @Override
            public void onMarkerClick(int pos, String title) {
            }

            @Override
            public void onLocation(Double Lat, Double Log, String string) {
                if (mFinalLatLng.longitude != 0 && mFinalLatLng.latitude != 0) return;
                mFinalLatLng = new LatLng(Lat, Log);
                binding.storagePosition.setText(string);
            }

            @Override
            public void getAddress(Double Lat, Double Log, String string) {
                super.getAddress(Lat, Log, string);
                mFinalLatLng = new LatLng(Lat, Log);
                binding.storagePosition.setText(string);
            }

            @Override
            public void onRegeocodeSearched(Double Lat, Double Log, String string) {
                super.onRegeocodeSearched(Lat, Log, string);
                binding.storagePosition.setText(string);
            }
        });
    }

    private void initPoiSearch() {

        if (getIntent().hasExtra(LATITUDE)) {
            latitude = getIntent().getDoubleExtra(LATITUDE, 0);
            LogUtil.d(TAG, "intentValue latitude:" + latitude);
        }
        if (getIntent().hasExtra(LONGTITUDE)) {
            longtitude = getIntent().getDoubleExtra(LONGTITUDE, 0);
            LogUtil.d(TAG, "intentValue longtitude:" + longtitude);
        }
        if (latitude != 0 && longtitude != 0) {
            mFinalLatLng = new LatLng(latitude, longtitude);
            binding.mMapView.setMyLocation(latitude, longtitude, R.mipmap.pick_location_tag);
        }
        binding.storagePosition.setText(city);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.iv_delete:
                binding.llButtomLayout.setVisibility(View.GONE);
                break;
            case R.id.txt_nav:
                binding.mapLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.map_baidu:
                if (MapUtils.isBdMapInstalled()) {
                    NavUtil.navigateForDestination(this, NavUtil.BAIDU, city, String.valueOf(latitude), String.valueOf(longtitude), "");
                } else {
                    MToast.showToast(StorageAddressActivity.this, "当前手机未安装百度地图");
                }

                break;
            case R.id.map_gaode:
                if (MapUtils.isGdMapInstalled()) {
                    NavUtil.navigateForDestination(this, NavUtil.GAODE, city, String.valueOf(latitude), String.valueOf(longtitude), "");
                } else {
                    MToast.showToast(StorageAddressActivity.this, "当前手机未安装高德地图");
                }

                break;
            case R.id.map_tenxun:
                if (MapUtils.isTencentMapInstalled()) {
                    NavUtil.navigateForDestination(this, NavUtil.TENXUN, city, String.valueOf(latitude), String.valueOf(longtitude), "");
                } else {
                    MToast.showToast(StorageAddressActivity.this, "当前手机未安装腾讯地图");
                }

                break;
            case R.id.cancel_btn:
                binding.mapLayout.setVisibility(View.GONE);
                break;
        }
    }

    public static void closeKeybord(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }
}
